package com.silence.gankio.mvvm.viewModel

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import com.silence.gankio.bean.GankIoData
import com.silence.gankio.core.net.HttpUtil
import com.silence.gankio.net.GankIoObjectObserver
import com.silence.gankio.service.GankIoService
import com.silence.gankio.sp.SplashSP

class SplashViewModel : BaseViewModel() {

    private val imageUrl by lazy { MutableLiveData<String>() }

    fun getImageUrl(): LiveData<String> {
        return imageUrl
    }

    fun loadImageUrl() {
        load(true, HttpUtil().getService(GankIoService::class.java).福利(1, 1), object :
            GankIoObjectObserver<GankIoData>(this) {
            override fun onSuccess(t: List<GankIoData>?) {
                if (t.isNullOrEmpty()) {
                    errorMsg.value = "数据获取出错！"
                } else {
                    val imageUrl = t[0].url
                    if (SplashSP.INSTANCE.getImageUrl() != imageUrl) {
                        SplashSP.INSTANCE.setImageUrl(imageUrl)
                        this@SplashViewModel.imageUrl.value = imageUrl
                    }
                }

            }
        })
    }

}
